package com.qingfeng.medical.controller.front.order;

import cn.hutool.core.map.MapUtil;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.biz.order.domain.entity.PaymentInfo;
import com.qingfeng.medical.biz.order.domain.enums.PaymentTypeEnum;
import com.qingfeng.medical.biz.order.service.PaymentInfoService;
import com.qingfeng.medical.biz.order.service.WeixinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;


/**
 * 支付信息表
 *
 * @author 清风学Java
 * @date 2023-05-11 13:43:46
 */
@Api(tags = "支付信息表管理")
@RestController
@RequestMapping("/api/order/weixin")
@CrossOrigin
@Validated
public class PaymentInfoApiController  extends BaseController {

    @Autowired
    private PaymentInfoService paymentInfoService;
    @Autowired
    private WeixinService weixinService;

    @ApiOperation(value = "下单生成二维码", notes = "下单生成二维码")
    @GetMapping("/createNative/{orderId}")
    @Log(title = "支付信息管理", businessType = BusinessType.INSERT, message = "下单生成二维码")
    public Result createNative(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @PathVariable("orderId") Long orderId) {
        return Result.ok(weixinService.createNative(orderId));
    }

    @ApiOperation(value = "查询支付状态", notes = "查询支付状态")
    @GetMapping("/queryPayStatus/{orderId}")
    @Log(title = "支付信息管理", businessType = BusinessType.INSERT, message = "查询支付状态")
    public Result queryPayStatus(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @PathVariable("orderId") Long orderId) {
        //调用微信接口实现支付状态查询
        Map<String, String> resultMap = weixinService.queryPayStatus(orderId, PaymentTypeEnum.WECHAT);
        //出错
        if (MapUtil.isEmpty(resultMap)) {
            return Result.fail().message("支付出错");
        }
        System.out.println("resultMap: "+resultMap);
        //如果成功
        if ("SUCCESS".equals(resultMap.get("trade_state"))) {
            System.out.println("扫码啦----------------------------");
            //更改订单状态，处理支付结果
            String outTradeNo = resultMap.get("out_trade_no");
            paymentInfoService.paySuccess(outTradeNo, PaymentTypeEnum.WECHAT, resultMap);
            System.out.println("结束啦--------------------");
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }






    @ApiOperation(value = "支付信息表分页列表", notes = "支付信息表分页列表")
    @GetMapping("/list")
    @Log(title = "支付信息表", businessType = BusinessType.SELECT, message = "支付信息表分页列表")
    public Result list(){

        return Result.ok();
    }

    @ApiOperation(value = "支付信息表信息", notes = "支付信息表信息")
    @GetMapping("/info/{id}")
    @Log(title = "支付信息表", businessType = BusinessType.SELECT, message = "支付信息表信息")
    public Result info(@PathVariable("id") Integer id){
		PaymentInfo paymentInfo = paymentInfoService.getById(id);

        return Result.ok();
    }

    @ApiOperation(value = "保存支付信息表信息", notes = "保存支付信息表信息")
    @PostMapping("/save")
    @Log(title = "支付信息表", businessType = BusinessType.SELECT, message = "保存支付信息表信息")
    public Result save(@RequestBody PaymentInfo paymentInfo){
		paymentInfoService.save(paymentInfo);

        return Result.ok();
    }

    @ApiOperation(value = "修改支付信息表信息", notes = "修改支付信息表信息")
    @PutMapping("/update")
    @Log(title = "支付信息表", businessType = BusinessType.SELECT, message = "修改支付信息表信息")
    public Result update(@RequestBody PaymentInfo paymentInfo){
		paymentInfoService.updateById(paymentInfo);

        return Result.ok();
    }

    @ApiOperation(value = "删除支付信息表信息", notes = "删除支付信息表信息")
    @DeleteMapping("/delete")
    @Log(title = "支付信息表", businessType = BusinessType.SELECT, message = "删除支付信息表信息")
    public Result delete(@RequestBody Integer[] ids){
		paymentInfoService.removeByIds(Arrays.asList(ids));

        return Result.ok();
    }

}
